package com.ingenio.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.ingenio.Adapter;
import com.ingenio.business.Rol;
import com.ingenio.business.Usuario;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class RolAdapter extends Adapter{
	
	public void agregarRol(Rol rol) throws Exception{
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
	    	conn = (Connection) abrirConexion();
	    	String add ="INSERT into rol (descRol, rolHabilitado) " +
					"VALUES (?, 1)";
			stmt = (PreparedStatement) conn.prepareStatement(add);
			stmt.setString(1, rol.getDescRol());
			stmt.execute();
		}
    	catch (Exception e) {
			e.printStackTrace();
			throw new Exception("Error al conectar con la base de datos.", e);
		}
    	finally{
    		cerrarConexion(conn);
    	}
	}
	
	public void eliminarRol(Rol rol) throws Exception{
    	Connection conn = null;
    	PreparedStatement stmt = null;
    	try {
	    	conn = (Connection) abrirConexion();
	    	String del ="UPDATE rol SET rolHabilitado = 0 WHERE IdRol = ?";
			stmt = (PreparedStatement) conn.prepareStatement(del);
			stmt.setInt(1, rol.getIdRol());
			stmt.execute();
		}
    	catch (Exception e) {
			e.printStackTrace();
			throw new Exception("Error al conectar con la base de datos.", e);
		}
    	finally{
    		cerrarConexion(conn);
    	}
    }
	
	public void modificarDescripcion(Rol rol) throws Exception{
    	Connection conn = null;
    	PreparedStatement stmt = null;
    	try {
	    	conn = (Connection) abrirConexion();
	    	String upd ="UPDATE rol SET descRol = ? WHERE IdRol = ?";
	    	stmt = (PreparedStatement) conn.prepareStatement(upd);
	    	stmt.setString(1, rol.getDescRol());
	    	stmt.setInt(2, rol.getIdRol());
	    	stmt.executeUpdate();
		}
    	catch (Exception e) {
			e.printStackTrace();
			throw new Exception("Error al conectar con la base de datos.", e);
		}
    	finally{
    		cerrarConexion(conn);
    	}
    }
	
	public Rol devolverRol(Integer id) throws Exception{
    	Connection conn = null;
    	Rol r = null;
    	PreparedStatement stmt = null;
    	PermisoAdapter pa = new PermisoAdapter();
    	try {
    		conn = (Connection) abrirConexion();
    		String sel ="SELECT descRol as descripcion FROM rol WHERE IdRol = ? and rolHabilitado = 1";
    		stmt = (PreparedStatement) conn.prepareStatement(sel);
    		stmt.setInt(1, id);
    		ResultSet result = stmt.executeQuery();
    		
    		if(result.next()){
    			String descripcion = result.getString("descripcion");
    			r = new Rol(id, descripcion, true);
    			r.setPermisos(pa.devolverPermisosRol(r));
    		}
    	}
    	catch (Exception e) {
			e.printStackTrace();
			throw new Exception("Error al conectar con la base de datos.", e);
		}
    	finally{
    		cerrarConexion(conn);
    	}
		return r;
    }
	
	public List<Rol> devolverTodos() throws Exception{
    	Connection conn = null;
    	List<Rol> roles = new ArrayList<Rol>();
    	PreparedStatement stmt = null;
		try {
			conn = (Connection) abrirConexion();
    		Rol r = null;
    		String sel ="SELECT IdRol , descRol as descripcion from rol WHERE rolHabilitado = 1";
    		stmt = (PreparedStatement) conn.prepareStatement(sel);
    		ResultSet result = stmt.executeQuery();
    		while(result.next()){
    			Integer id = result.getInt("IdRol");
    			String descripcion = result.getString("descripcion");
    			r = new Rol(id, descripcion, true);
    			roles.add(r);
    		}
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new Exception("Error al conectar con la base de datos.", e);
		}
    	finally{
    		cerrarConexion(conn);
    	}
    	return roles;
    }
	
	public List<Rol> devolverRolesUsuario(Usuario usuario) throws Exception{
    	Connection conn = null;
    	List<Rol> roles = new ArrayList<Rol>();
    	RolAdapter ra = new RolAdapter();
    	PreparedStatement stmt = null;
		try {
			conn = (Connection) abrirConexion();
    		Rol r = null;
    	
    		String sel ="SELECT * from usuariosroles WHERE IdUsuario = ?";
    		stmt = (PreparedStatement) conn.prepareStatement(sel);
    		stmt.setInt(1, usuario.getIdUsuario());
    		ResultSet result = stmt.executeQuery();
    		while(result.next()){
    			Integer idRol = result.getInt("IdRol");
    			r = ra.devolverRol(idRol);
    			if (r.isPerRol()){
    				roles.add(r);
    			}
    		}
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new Exception("Error al conectar con la base de datos.", e);
		}
    	finally{
    		cerrarConexion(conn);
    	}
    	return roles;
    }
}
